# Why doesn't js file work in Google Chrome? 
#
# Clicking Serial number link will show ticket history:
# http://localhost/rt/webrt.cgi/webrt.cgi?serial_num=94710&display=History
#
# Clicking email link will enable user to reply to ticket requestor:
# http://localhost/rt/webrt.cgi/webrt.cgi?serial_num=94710&refresh_req=true&transaction=0&display=SetReply;
#
# http://localhost/rt/webrt.cgi/webrt.cgi/webrt.cgi?display=SetStatus&serial_num=94690

# Define some CSS
$pageCSS = '
        <style type="text/css">

                .dropDownCell, .inputTypeCell{
                        height: 40px;
                        }
                        
                .dropDownCell, #edit > font{
                        cursor: hand;
                        }
                        
                #genTextArea{
                        width: 100%;
                        border: solid 1px gray;
                        }
                
                #msgDisplay{
                        height: 30px;
                        position: absolute;
                        text-align: center;
                        }
                
                #msgDisplay > font{
                        padding-left: 10px;
                        padding-right: 10px;
                        background-color: #A2CD5A !important;
                        font-size: 15px;
                        color: #000000;
                        }
                
        </style>'

jQuery ($) ->

	$(document).on 'scroll ready', ->

		$('font[size="-1"] > table').dbg()
		###
		$('#msgDisplay').css('top' : window.pageYOffset)
		$('#msgDisplay').css('width' : window.innerWidth)
		###
		return

	Deep = true

	# Define a regex to match a ticket's serial number
	serialNum = /serial_num=(\d+)/
	rt_link = "/rt/webrt.cgi"

		
	# We will try to get webrt's main page from the server.
	#
	# >FIXME
	# We need to provide a load spinner.
	#
	$.get rt_link, (data, textStatus, jqXHR) ->

		# The data response is an HTML page.
		# We will use it to replace the starting HTML page.
		#
		# >FIXME
		# Result lacks a body tag
		#
		$('html').html data
			
		# Every time the user clicks an anchor tag, we need to intercept it and
		# get the href ourself, otherwise, we will lose our Javascript file.
		$('html').on 'click', 'a', ->
			href = $(@).prop 'href'
			$.get href, (data) ->
				$('html').html data
			return false

		# The main page will be divided into several tables representing the major sections:
		#
		# * table header showing ticket properties
		# * table body listing tickets
		# * search form
		# * function bar
		# * footer
		#
		# >FIXME
		# Let's give an identifier to the sections, so we will locate them
		# easily.
		$('body >font >table >tbody >tr').prop 'id', 'header'
		$('body >font >table >tbody').prop 'id', 'tickets'
		$('body >form').prop 'id', 'search_form'
		$('body >table').prop 'id', 'function_bar'
		$('body >center').prop 'id', 'footer'

		# The main table will list one ticket per row with a core set of
		# properties, eg, priority, status, subject.  The major interactivity
		# of the main table will be that the user should be able to change the
		# value of an property by clicking it to enter an editing mode.
		# Let's adorn each table row with a class name and a serial number.
		$('font >table >tbody >tr').each ->
			$this = $(@)
			href = $('a', $this).attr 'href'
			if sn = (href.match serialNum)?[1]
				$this.addClass('ticket').attr 'id': sn
				$this.children('td:nth-child(3)').attr('id', 'status')
				$this.children('td:nth-child(8)').attr('id', 'area')
				$this.children('td:nth-child(5)').attr('id', 'subject')
				$this.children('td:nth-child(9)').attr('id', 'requestor')

		$('font >table >tbody >tr').rtTickets()
		$('font >table >tbody >tr').dropdown()

		return # from get handler

	return
